package com.quick.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.common.Exception.exception.BaseException;
import com.quick.common.bean.Order;
import com.quick.common.service.IOrderService;
import com.quick.order.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@DubboService
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Override
    public int addOrder(Order order) {
        // 1. 强制清空id，确保数据库自增生效
        order.setId(null);
        // 2. 判断save是否成功，失败抛异常
        boolean saveSuccess = super.save(order);
        if (!saveSuccess) {
            throw new BaseException(400,"订单保存失败"); // 抛异常中断流程，避免后续传null
        }
        // 3. 确保id不为null（save成功后MyBatis-Plus会回填id）
        if (order.getId() == null) {
            throw new BaseException(400,"订单ID回填失败");
        }
        return order.getId();
    }

    @Override
    public void editStatus(int id, int status) {
        super.update().set("status",status).eq("id",id).update();
    }

    /**
     * 设置订单的司机
     * @param id 订单id
     * @param driverId  接单司机id
     */
    @Override
    public void setDriver(int id, int driverId) {
        super.update().set("driver_id",driverId).eq("id",id).update();
    }


    @Override
    public List<Order> getOrderList() {
        return super.list();
    }

    @Override
    public Order getOrderById(int id) {
        return super.getById(id);
    }

    @Override
    public int updateOrder(Order order) {
        return super.updateById(order) ? 1 : 0;
    }

    @Override
    public IPage<Order> getPage(Order order, IPage< Order>  page) {
        if (page == null){
            page = new Page<>(1,10);
        }
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getUser_id, order.getUser_id())
                .eq(Order::getStatus, order.getStatus())
                .eq(Order::getIsdel, 0);
        return super.page(page, queryWrapper);
    }

    @Override
    public IPage<Order> getPage1(Order order, IPage<Order> page) {
        if (page == null){
            page = new Page<>(1,10);
        }
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getDriver_id, order.getDriver_id())
                .eq(Order::getStatus, order.getStatus())
                .eq(Order::getIsdel, 0);
        return super.page(page, queryWrapper);
    }

    @Override
    public int deleteOrder(int id) {
        return super.removeById(id) ? 1 : 0;
    }

    @Override
    public int deleteOrderList(List<Integer> ids) {
        return 0;
    }

    @Override
    public Order getOrderByUserId(int user_id) {
        return null;
    }
}
